package com.scalar.chiptrack.operations.process.cycleTime.actioncommands;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.cycleTime.dao.StageCycleTimeDAO;
import com.scalar.chiptrack.operations.process.cycleTime.forms.StageCycleTimeForm;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.utils.Logger;

/**
 * Created By Ganesh
 * Date  : Apr 3, 2006
 * Time  : 2:59:05 PM
 */
public class StageCycleTimeSaveCycleTimeDetailsCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/stageCycleTime.do";

    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
    {
        StageCycleTimeForm stageCycleTimeForm = ( StageCycleTimeForm ) form;

        HashMap errors = new HashMap();
        HashMap info   = new HashMap();
        String action  = request.getParameter ( "action" );
        DBTransaction dbTransaction = null;
        boolean success = false;

        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );


        HttpSession mSession = request.getSession();
        User user            = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }
        else
        {
            if ( user.isActionAllowed ( Functionality.STAGE_CYCLE_TIME_SETUP , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute ( "showMessage", "FALSE" );
            }
            else
            {
                request.setAttribute ( "showMessage", "TRUE" );
            }
        }

        try
        {
           ArrayList vendorLocationsList              = StageCycleTimeDAO.loadAssemblyVendorLocations(null);
           ArrayList vendorLocationsLableValueBeans   = new ArrayList();
           int vendorLocationsListSize                = vendorLocationsList.size();
           VendorLocation vendorLocation              = null;
           ArrayList stageDetails;
           dbTransaction = DBTransaction.getInstance();
           dbTransaction.begin();

           if (vendorLocationsList != null)
           {
               for( int i = 0; i < vendorLocationsListSize; i++ )
               {
                   vendorLocation= (VendorLocation) vendorLocationsList.get(i);
                   vendorLocationsLableValueBeans.add ( new org.apache.struts.util.LabelValueBean( vendorLocation.getLocationName(), vendorLocation.getLocationID()+"") );
               }

           }
           if (stageCycleTimeForm.getStageDetails().size() != -1 )
           {
               stageDetails= stageCycleTimeForm.getStageDetails();
               StageCycleTimeDAO.saveAssemblyStages( dbTransaction, stageDetails );
           }


            request.setAttribute ( "locationsList", vendorLocationsLableValueBeans );
            success = true;

        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        finally
        {
            if( dbTransaction != null )
            {
                if( success == true )
                {
                    try
                    {
                        dbTransaction.commit();
                        info.put ( "Assembly Stage Cycle Time Information Saved Successfully ", null );
                    }
                    catch( Exception e )
                    {
                        e.printStackTrace();
                        Logger.debug( "Error while committing the database update  " );
                    }

                }
                else
                {
                    try
                    {
                        dbTransaction.rollback();
                        errors.put ( "Assembly Stage Cycle Time Information Aborted", null );
                    }
                    catch( Exception e )
                    {
                        e.printStackTrace();
                        Logger.debug( "Error while rolling back the Lot Information update" );
                    }
                }
            }
        }

        stageCycleTimeForm.setLocationId( -1 );
        stageCycleTimeForm.setProcessstepId( -1 );
        stageCycleTimeForm.setStageDetails( null );


        request.setAttribute ( "action", action );

        return mapping.findForward( "success" );

        }


}
